﻿using Msung.Core.Kafka.Integration;
using Newtonsoft.Json;

namespace Msung.Core.Extensions
{
    public static class RecieveResultExtension
    {

        public static T? GetRecieveResultMessage<T>(this RecieveResult recieveResult) where T : class
        {
            if (recieveResult == null) return null;
            var eventData = (Newtonsoft.Json.Linq.JContainer)JsonConvert.DeserializeObject(recieveResult.Message.Replace("undefined", ""));
            var data = eventData.First.First;
            var dataModel = JsonConvert.DeserializeObject<T>(data.ToString());
            return dataModel;
        }

        public static T? GetRecieveMessage<T>(this RecieveResult recieveResult) where T : class
        {
            if (recieveResult == null) return null;
            var eventData = (Newtonsoft.Json.Linq.JContainer)JsonConvert.DeserializeObject(recieveResult.Message.Replace("undefined",""));
            var dataModel = JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(eventData));
            return dataModel;
        }
    }
}
